﻿/*
 * Copyright 2011 www.jhpeng.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Data.Common;
using System.Collections.Generic;

using System.Xml;

namespace SimpleService.Database
{
    /// <summary>
    /// 實現db操作物件
    /// </summary>
    public class DbOperation : IDbOperation
    {
        public IDbProvider Provider = null;

        /// <summary>
        /// 建立 Db 物件
        /// </summary>
        /// <param name="type"></param>
        public DbOperation(string type)
        {
                Provider = DbProvider.Get(type);
        }

        /// <summary>
        /// 取得 Connection
        /// </summary>
        /// <returns></returns>
        public DbConnection GetConnection()
        {
            DbConnection conn = Provider.CreateConnection();
            return conn;
        }

        /// <summary>
        /// 取得 Connection
        /// </summary>
        /// <returns></returns>
        public DbConnection GetConnection(string connectionString)
        {
            DbConnection conn = Provider.CreateConnection();
            conn.ConnectionString = connectionString;
            return conn;
        }

        /// <summary>
        /// 取得 Command
        /// </summary>
        /// <returns></returns>
        public DbCommand GetCommand()
        {
            DbCommand cmd = Provider.CreateCommand();
            return cmd;
        }

        /// <summary>
        ///  取得 Command
        /// </summary>
        /// <param name="sqlText"></param>
        /// <param name="conn"></param>
        /// <returns></returns>
        public DbCommand GetCommand(string sqlText, DbConnection conn)
        {
            DbCommand cmd = Provider.CreateCommand();
            cmd.CommandText = sqlText;
            cmd.Connection = conn;
            return cmd;
        }

        /// <summary>
        /// 取得 DataAdapter
        /// </summary>
        /// <returns></returns>
        public DbDataAdapter GetDataAdapter()
        {
            DbDataAdapter adapter = Provider.CreateDataAdapter();
            return adapter;
        }

        /// <summary>
        /// 取得 DataAdapter
        /// </summary>
        /// <param name="sqlText"></param>
        /// <param name="conn"></param>
        /// <returns></returns>
        public DbDataAdapter GetDataAdapter(DbCommand cmd)
        {
            DbDataAdapter adapter = Provider.CreateDataAdapter();
            adapter.SelectCommand = cmd;
            return adapter;
        }

        /// <summary>
        /// 取得 Transaction
        /// </summary>
        /// <param name="conn"></param>
        /// <returns></returns>
        public DbTransaction GetTransaction(DbConnection conn)
        {
            return conn.BeginTransaction();
        }

    
    }
}
